package com.meichis.yndms.vm;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.meichis.mcsappframework.entity.AppVersion;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.model.Notice;
import com.meichis.yndms.model.RotateAdv;
import com.meichis.yndms.model.UserInfo;
import com.meichis.yndms.model.YIQIYing;
import com.meichis.yndms.repository.manager.HomeFragmentManager;
import com.meichis.yndms.repository.manager.MainRequestManager;

import java.util.Calendar;
import java.util.List;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-03-10 11:30
 */
public class MainVM extends MyAndroidViewModel {


    private MutableLiveData<UserInfo> userInfo;
    private RotateAdv rotateAdv = null;
    private Context context;
    private MutableLiveData<AppVersion> appVersion;
    //判断识别照片是否上传成功
    private MutableLiveData<Boolean> uploadPic;
    //未读公告
    private MutableLiveData<List<Notice>> noticeList;
    private MutableLiveData<Integer> feedBackCount;
    private MutableLiveData<Boolean> hasShowFeedBackDialog;
    private MutableLiveData<YIQIYing> yiQiYing;

    public MutableLiveData<YIQIYing> getYiQiYing() {
        if (yiQiYing == null) {
            yiQiYing = new MutableLiveData<>();
        }
        return yiQiYing;
    }

    public MutableLiveData<List<Notice>> getNoticeList() {
        if (noticeList == null) {
            noticeList = new MutableLiveData<>();
        }
        return noticeList;
    }

    public MutableLiveData<Boolean> getUploadPic() {
        if (uploadPic == null) {
            uploadPic = new MutableLiveData<>();
        }
        return uploadPic;
    }

    public MutableLiveData<Integer> getFeedBackCount() {
        if (feedBackCount == null) {
            feedBackCount = new MutableLiveData<>();
        }
        return feedBackCount;
    }

    public MutableLiveData<Boolean> getHasShowFeedBackDialog() {
        if (hasShowFeedBackDialog == null) {
            hasShowFeedBackDialog = new MutableLiveData<>();
            hasShowFeedBackDialog.setValue(false);
        }
        return hasShowFeedBackDialog;
    }

    public MainVM(@NonNull Application application) {
        super(application);
        this.context = application.getApplicationContext();
        getYiQiYing();
        getNoticeList();
        getUploadPic();
        getFeedBackCount();
        getHasShowFeedBackDialog();
        getUserInfo();
        getAppVersion();


    }

    public MutableLiveData<UserInfo> getUserInfo() {
        UserInfo userInfoObj = null;
        if (userInfo == null || userInfo.getValue() == null) {
            String stringValue = util.getString(SharePreferenceUtil.PREFERENCES_USERINFO);
            if (!TextUtils.isEmpty(stringValue)) {
                userInfoObj = new Gson().fromJson(stringValue, UserInfo.class);
            }
            userInfo = new MutableLiveData<>();
            userInfo.setValue(userInfoObj);
        }
        return userInfo;
    }

    public MutableLiveData<AppVersion> getAppVersion() {
        if (appVersion == null) {
            appVersion = new MutableLiveData<>();
        }
        return appVersion;
    }

    public RotateAdv getRotateAdv() {
        if (rotateAdv == null) {
            rotateAdv = new Gson().fromJson(util.getStringValue(SharePreferenceUtil.PREFERENCES_ROTATEADV), RotateAdv.class);
        }
        return rotateAdv;
    }

    public void GetUnReadNotice() {
        HomeFragmentManager.getInstance().GetUnReadNotice(
                getShowDialog(), getUserInfo().getValue().getOrgId(),
                getUserInfo().getValue().getId(), DateUtil.getMultipleMonth(Calendar.getInstance().getTime(), -6, DateUtil.FORMAT_YMD),
                DateUtil.getCurDateStr(DateUtil.FORMAT_YMD), getNoticeList());
    }


    /**
     * 检查版本更新
     */
    public void CheckUpdate() {
        MainRequestManager.getInstance().GetLastVersionInfo(getShowDialog(),
                context.getResources().getString(R.string.app_code),
                getAppVersion());
    }


    /**
     * 看板数据
     */
    public void GetOverview() {
        MainRequestManager.getInstance().GetOverview(getShowDialog(), getUserInfo().getValue().getOrgId(),
                getUserInfo().getValue().getId(), new MutableLiveData<>());
    }

    /**
     * 终端反馈待处理消息
     */
    public void GetFeedbackPopCount() {
        MainRequestManager.getInstance().GetFeedbackPopCount(getShowDialog(), getUserInfo().getValue().getOrgId(), getFeedBackCount());
    }

    /**
     * 获取附件下载地址
     */
    public void GetAttachmentDownloadURL() {
        MainRequestManager.getInstance().GetAttachmentDownloadURL("", getUserInfo().getValue().getOrgId());
    }

    public void GetYQYLinkUrl() {
        MainRequestManager.getInstance().GetYQYLinkUrl(getShowDialog(), getYiQiYing());
    }
}
